package cn.lingyangwl.agile.system.controller;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.lingyangwl.agile.common.security.annotation.*;
import cn.lingyangwl.agile.file.excel.*;
import cn.lingyangwl.agile.ram.*;
import cn.lingyangwl.agile.system.model.entity.*;
import cn.lingyangwl.agile.system.model.rqrs.log.*;
import cn.lingyangwl.agile.system.service.*;
import cn.lingyangwl.framework.core.response.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 访问日志记录
 * 
 * @author shenguangyang
 */
@Api(tags = "访问日志管理")
@RestController
@RequestMapping("/accessLog")
public class AccessLogController {
    @Resource
    private SysAccessLogService accessLogService;

    @ApiOperation("获取日志列表")
    @GetMapping("/page")
    @RamResource(action = "sys:accessLog:query", name = "查询访问日志", type = ActionType.READ)
    public Resp<IPage<SysAccessLog>> page(AccessLogPageReq req) {
        IPage<SysAccessLog> page = accessLogService.pageAccessLog(req);
        return Resp.ok(page);
    }

    @RamResource(action = "sys:accessLog:export", name = "导出访问日志", type = ActionType.READ)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysAccessLog reqParam) throws Exception {
        List<SysAccessLog> list = accessLogService.listAccessLog(reqParam);
        ExportParams params = new ExportParams();
        params.setType(ExcelType.HSSF);
        CusExcelExportUtil.exportExcel(response, params, "访问日志.xls", SysAccessLog.class, list);
    }

    @ApiOperation("移除日志")
    @RamResource(action = "sys:accessLog:delete", name = "删除访问日志", type = ActionType.WRITE)
    @DeleteMapping("/deleteBatch")
    public Resp<?> delete(@RequestParam(value = "ids") Long[] operateIds) {
        return accessLogService.deleteBatch(operateIds) ? Resp.ok() : Resp.fail("删除失败");
    }

    @RamResource(action = "sys:accessLog:delete", name = "删除访问日志", type = ActionType.WRITE)
    @DeleteMapping("/delete")
    public Resp<?> delete(@RequestParam(value = "id") Long operateId) {
        return accessLogService.deleteBatch(new Long[] { operateId }) ? Resp.ok() : Resp.fail("删除失败");
    }

    @ApiOperation("清空日志")
    @RamResource(action = "sys:accessLog:clean", name = "清除访问日志", type = ActionType.WRITE)
    @DeleteMapping("/clean")
    public Resp<?> clean() {
        accessLogService.clear();
        return Resp.ok();
    }

    @ApiOperation("定时删除日志")
    @PostMapping("/deleteLogTask")
    public Resp<Integer> deleteLogTask(@RequestParam("endTime") String endTime) {
        return Resp.ok(accessLogService.deleteLogTask(endTime));
    }

    @ApiOperation("添加日志")
    @InnerAuth
    @PostMapping("/save")
    public Resp<?> save(@RequestBody AccessLogSaveReq req) {
        accessLogService.save(req);
        return Resp.ok();
    }
}